package com.sise.controller;


import com.sise.pojo.Student;
import com.sise.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 彭海霞
 * @since 2020-06-13
 */
@Controller
@RequestMapping("/student")
public class StudentController {


    @Autowired
    private StudentService studentService;

    /**
     * 分页查询
     */
    @RequestMapping("/selectStudentAll")
    public String selectStudentAll(Model model) {
        //  参数一：当前页
        //  参数二：页面大小
        //  使用了分页插件之后，所有的分页操作也变得简单的！
        List<Student> list = studentService.list(null);
        model.addAttribute("list", list);
        return "student-list";
    }

    @RequestMapping("/addStudent")
    public String addStudent(String sname, String sex, String birthday, int age, String major, String dorm, MultipartFile img, Model model) {
        System.out.println(sname +" "+sex +" "+birthday +" "+age +" "+major +" "+dorm +" "+img);
        try {
            //文件上传的地址
            String path = "E:/IdeaProjects/sm/src/main/resources/static/upload";
            ;
            System.out.println(path);
            //获取文件的名称
            System.out.println(img.getOriginalFilename());
            final String fileName = UUID.randomUUID() + img.getOriginalFilename();
            System.out.println(fileName);
            //限制文件上传的类型
            String contentType = img.getContentType();
            File file = new File(path, fileName);
            System.out.println(file.toString());
            img.transferTo(file);
            Student student=new Student().setSname(sname).setSex(sex).setBirthday(birthday).setAge(age).setMajor(major).setDorm(dorm).setImg("http://localhost:8080/upload/" + fileName);
            System.out.println(student);
            studentService.save(student);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "forward:/student/selectStudentAll";
    }
    @RequestMapping("/selectStudentBySid")
    public String selectStudentBySid(Integer sid,Model model) {
        System.out.println(sid);
        Student student = studentService.getById(sid);
        model.addAttribute("student", student);
        return "student-view";
    }
    @RequestMapping("/updateStudent")
    public String updateStudent(Student stu) {
        System.out.println(stu);
        boolean b = studentService.updateById(stu);
        return "forward:/student/selectStudentAll";
    }
    @RequestMapping("/deleteStudentBySid")
    public String deleteStudentBySid(Integer sid) {
        boolean b = studentService.removeById(sid);
        return "forward:/student/selectStudentAll";
    }
}

